package cn.wellwan.redislock;

import redis.clients.jedis.Jedis;

/**
 * Copyright (C),微动天下
 * FileName: RedisDistributedLockTest
 * Author:   maokai
 * Date:     2019/1/22 10:12
 * Description:
 */
//　　下面就以秒杀库存数量为场景，测试下上面实现的分布式锁的效果。具体测试代码如下：
//
public class RedisDistributedLockTest {
    static int n = 500;
    public static void secskill() {
        System.out.println(--n);
    }

    public static void main(String[] args) {
        Runnable runnable = () -> {
            RedisDistributedLock lock = null;
            String unLockIdentify = null;
            try {
                Jedis conn = new Jedis("101.37.85.135",6379);
                conn.auth("vdchina#Flzx3qc14yhl9t");
                lock = new RedisDistributedLock(conn, "test1");
                unLockIdentify = lock.acquire();
                System.out.println(Thread.currentThread().getName() + "正在运行");
                secskill();
            } finally {
                if (lock != null) {
                    lock.release(unLockIdentify);
                }
            }
        };

        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(runnable);
            t.start();
        }
    }
}